Imports CrystalDecisions.Shared
Partial Public Class FrmListadoFacturaVenta
    Inherits System.Web.UI.Page

    Public Factura As CapaDeNegocios.nFactura = New CapaDeNegocios.nFactura
    Public Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Public Remito As CapaDeNegocios.nRemitoServicios = New CapaDeNegocios.nRemitoServicios()
    Public DetalleFacturaVenta As CapaDeNegocios.nDetalleFacturaVenta = New CapaDeNegocios.nDetalleFacturaVenta()

    Public CategoriaFiscal As CapaDeNegocios.nCategoriaFiscal = New CapaDeNegocios.nCategoriaFiscal

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If

        If Not Page.IsPostBack Then
            HfIdFactura.Value = Request.QueryString("IdFactura")
            If (HfIdFactura.Value.ToString() <> "") Then
                LnkBuscar.Visible = False
                GridFactura.DataSource = Factura.BuscarFiltrado(HfIdFactura.Value.ToString(), "", "", "", "", "", "", "", "", "")
                GridFactura.DataBind()
                BtnImprimir.Visible = True
            Else
                LLenarCboClientes()
                LLenarCboCategorias()
                BtnImprimir.Visible = False
            End If
            BtnImprimir.OnClientClick = "javascript:var popup = window.open('../Reportes/RptListadoDeFacturas.aspx',(new Date().getTime()),'Width=900,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
        End If
    End Sub
    Sub LLenarCboClientes()
        CboClientes.Items.Clear()
        CboClientes.Items.Add(New ListItem("Ninguno...", -1))
        CboClientes.DataSource = Cliente.Listar("", "", "", "", "", "", "", "", "", "", "", "")
        CboClientes.DataTextField = "RazonSocial"
        CboClientes.DataValueField = "CodigoCliente"
        CboClientes.DataBind()
        If CboClientes.Items.Count > 0 Then
            CboClientes.SelectedIndex = 0
        End If
    End Sub
    Sub LLenarCboCategorias()
        CboCategorias.Items.Clear()
        CboCategorias.Items.Add(New ListItem("Ninguno...", -1))
        CboCategorias.DataSource = CategoriaFiscal.Buscar("", "")
        CboCategorias.DataTextField = "Detalle"
        CboCategorias.DataValueField = "CodigoCategoria"
        CboCategorias.DataBind()
        If CboCategorias.Items.Count > 0 Then
            CboCategorias.SelectedIndex = 0
        End If
    End Sub

    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        Dim Cliente As String = CboClientes.SelectedValue.ToString()
        Dim Categoria As String = CboCategorias.SelectedValue.ToString()
        If Cliente = "-1" Then
            Cliente = ""
        End If
        If Categoria = "-1" Then
            Categoria = ""
        End If
        GridFactura.DataSource = Factura.BuscarFiltrado("", "", "", "", "", "", "", Cliente, Categoria, "")
        GridFactura.DataBind()
        BtnImprimir.Visible = True
        Session("RptListadoDeFacturas") = GridFactura.DataSource
    End Sub

    Protected Sub GridFactura_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridFactura.RowCommand
        If e.CommandName = "Anular" Then
            If e.CommandSource.Parent.Parent.FindControl("LblEstado").text.ToString() = "EMITIDA" Then
                Factura.Modificar(e.CommandSource.Parent.Parent.FindControl("HFIdFactura").Value.ToString(), "", "", "ANULADA", "", "", "", "", "", "")
                Dim DtsRemito As DataSet = DetalleFacturaVenta.BuscarFiltrado("", "", "", "", e.CommandSource.Parent.Parent.FindControl("HFIdFactura").Value.ToString(), "", "")
                Dim i As Integer
                For i = 0 To DtsRemito.Tables(0).Rows.Count - 1
                    If DtsRemito.Tables(0).Rows(i)("CodigoRemito").ToString() <> "" Then
                        Remito.Modificar(CInt(DtsRemito.Tables(0).Rows(i)("CodigoRemito").ToString()), 0, New DateTime, 0, 0, "", -1)
                    End If
                Next
                LnkBuscar_Click(sender, e)
            End If
        End If
    End Sub

    Protected Sub GridFactura_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridFactura.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim IntLnkSeleccion As LinkButton = e.Row.FindControl("LnkSeleccion")
            Dim IntLblEstado As Label = e.Row.FindControl("LblEstado")
            Dim IntLnkNC As LinkButton = e.Row.FindControl("LnkNC")
            Dim IntLnkND As LinkButton = e.Row.FindControl("LnkND")
            Dim IntLnkAnular As LinkButton = e.Row.FindControl("LnkAnular")


            Dim IntHFIdFactura As HiddenField = e.Row.FindControl("HFIdFactura")
            IntLnkSeleccion.OnClientClick = "javascript:var popup = window.open('FrmListadoDetalleFacturaVenta.aspx?IDFactura=" + IntHFIdFactura.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            IntLnkAnular.Visible = IntLblEstado.Text.ToString() = "EMITIDA"
            If (HfIdFactura.Value.ToString() <> "" Or IntLblEstado.Text = "CANCELADA") Then
                IntLnkNC.OnClientClick = "javascript:var popup = window.open('FrmNCND.aspx?IdTipo=NC&IdFactura=" + IntHFIdFactura.Value.ToString() + "&Trabado=Si',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
                IntLnkND.OnClientClick = "javascript:var popup = window.open('FrmNCND.aspx?IdTipo=ND&IdFactura=" + IntHFIdFactura.Value.ToString() + "&Trabado=Si',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
                IntLnkAnular.Visible = False
            Else
                IntLnkNC.OnClientClick = "javascript:var popup = window.open('FrmNCND.aspx?IdTipo=NC&IdFactura=" + IntHFIdFactura.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
                IntLnkND.OnClientClick = "javascript:var popup = window.open('FrmNCND.aspx?IdTipo=ND&IdFactura=" + IntHFIdFactura.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            End If

        End If
    End Sub

    Protected Sub GridFactura_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridFactura.PageIndexChanging
        GridFactura.PageIndex = e.NewPageIndex
        LnkBuscar_Click(sender, e)
    End Sub


    Protected Sub BtnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnImprimir.Click

    End Sub
End Class